
use		"./output/intermediate/cities_patents_wclass_19001929", clear

order pat_*, alpha
order npi_id f_myr, first

merge m:1 npi_id using "./output/intermediate/npis"
drop	_merge

xtset  	npi_id f_myr

gen		f_yr = year(dofm(f_myr))
gen		f_m = month(dofm(f_myr))

tempfile cities_pat
save	"`cities_pat'", replace

** Add in population data **
import 	delimited ".\input\population\wikipedia\wikipedia_1910_1920_43cities.csv", clear

merge 1:m city using "`cities_pat'"
drop 	_merge

xtset  	npi_id f_myr

** Interpolate population data **

gen		pop_int_0010 = pop1900 + (f_myr-tm(1900m4))*(pop1910-pop1900)/120
gen		pop_int_1020 = pop1910 + (f_myr-tm(1910m4))*(pop1920-pop1910)/120
gen		pop_int_2030 = pop1920 + (f_myr-tm(1920m4))*(pop1930-pop1920)/120

gen 	pop_int = pop_int_0010
replace	pop_int = pop_int_1020 if tin(1910m5,1920m4)
replace	pop_int = pop_int_2030 if tin(1920m5,1930m4)

lab var pop_int "Interpolated population (monthly using '00,'10,'20,'30 census pops)"

drop	pop_int_???? pop19??

** Create rates **
foreach v in first_inv all_inv wtd_inv {
	gen prate_`v' = pat_`v'/(pop_int/100000)
	
	foreach cl in 0 A B C D E F G H Y {	
		gen prate_`v'_`cl' = pat_`v'_`cl'/(pop_int/100000)
	}
}

compress

save 	"./output/cities_patents_npis_cats", replace
clear
